/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import org.aroundme.model.User;
import org.aroundme.persistency.UserManager;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.unitTools.UniqueIdGenerator;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;

public class UserManagerTest extends TestFixture {
	private UserManager userMan;
	
	protected void setUp() throws Exception {
		super.setUp();
		userMan = new UserManager();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		userMan = null;
	}

	public void testSaveUser() {
		User us = new User();
		us.setLoginName("" + new UniqueIdGenerator().getUniqueId());
		us.setPassword("mypass");
		us.setFirstname("Juri");
		us.setLastname("Str...");
		us.setEmail("email");
		
		//save it to the DB
		userMan.saveUser(us);
		
		//check if it is present there
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		User retrievedUser = null;
		s.beginTransaction();
		try{
			retrievedUser = (User) s.createQuery("from User where id=?").setInteger(0, us.getId()).uniqueResult();
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		assertNotNull(retrievedUser);
		assertEquals("Id of retrieved", us.getId(), retrievedUser.getId());
		assertEquals("Loginname", us.getLoginName(), retrievedUser.getLoginName());
		assertEquals("Password", us.getPassword(), retrievedUser.getPassword());
		assertEquals("Firstname", us.getFirstname(), retrievedUser.getFirstname());
		assertEquals("Lastname", us.getLastname(), retrievedUser.getLastname());
	}

	public void testGetUserByLoginName() {
		String loginName = "" + new UniqueIdGenerator().getUniqueId();
		User us = new User();
		us.setLoginName(loginName);
		us.setFirstname("Juri");
		us.setLastname("Str...");
		us.setEmail("email");
		
		//save it to the DB
		userMan.saveUser(us);
		
		//retrieve by Loginname
		User retrieved = userMan.getUserByLoginName(loginName);
		
		assertNotNull(retrieved);
		assertEquals("id", us.getId(), retrieved.getId());
		assertEquals("loginname", us.getLoginName(), retrieved.getLoginName());
	}

}
